package com.jeesite.modules.student.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.student.entity.Student;
import com.jeesite.modules.student.service.StudentService;

/**
 * 学生表Controller
 * @author ccz
 * @version 2025-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/student/student")
public class StudentController extends BaseController {

	@Autowired
	private StudentService studentService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Student get(String id, boolean isNewRecord) {
		return studentService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("student:student:view")
	@RequestMapping(value = {"list", ""})
	public String list(Student student, Model model) {
		model.addAttribute("student", student);
		return "modules/student/studentList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("student:student:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Student> listData(Student student, HttpServletRequest request, HttpServletResponse response) {
		student.setPage(new Page<>(request, response));
		Page<Student> page = studentService.findPage(student);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("student:student:view")
	@RequestMapping(value = "form")
	public String form(Student student, Model model) {
		model.addAttribute("student", student);
		return "modules/student/studentForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("student:student:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Student student) {
		studentService.save(student);
		return renderResult(Global.TRUE, text("保存学生表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("student:student:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Student student) {
		studentService.delete(student);
		return renderResult(Global.TRUE, text("删除学生表成功！"));
	}

	@RequestMapping(value = "batchDelete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchDelete(@RequestBody List<String> studentList) {
		Map<String, Object> result = new HashMap<>();
		try {
			studentService.batchDeleteByCondition(studentList); // 调用服务层方法
			result.put("result", "success");
			result.put("message", "删除成功");
		} catch (Exception e) {
			result.put("result", "error");
			result.put("message", "删除失败：" + e.getMessage());
		}
		return result;
	}
	
}